<?php
class  Musergroup extends CI_Model{
	function __construct(){
		parent::__construct();
	}
	function getUserGroupAll(){
		$arr = array();
		$sql = "select *
				from tbusergroup";
		$arr = array();		
		$query = $this->db->query($sql);
		foreach ($query->result() as $row){
			$arr[$row->id] = $row->name;
		}
		return $arr;
	}
	function loadUserGroup($name){
		$sql = "select *
				from tbusergroup";
		$arr = array();		
		if($name!=""){
			$sql.=" where name like ?";
			$arr[] = "%$name%";			
		}			
		$query = $this->db->query($sql,$arr);
		return $query->result();
	}
	function delUserGroup($id){
		$sql = "delete from tbusergroup
				where id in $id";
		$this->db->query($sql);
		return $this->db->affected_rows();
	}
	function checkExistsName($name){
		$sql = "select * from tbusergroup where name = ?";
		$query = $this->db->query($sql,array("$name"));
		foreach ($query->result() as $row){
			return true;
		}
		return false;
	}
	function checkExistsNameForUpdate($id,$name){
		$sql = "select * from tbusergroup where name = ? and id !=?";
		$query = $this->db->query($sql,array("$name","$id"));
		foreach ($query->result() as $row){
			return true;
		}
		return false;
	}
	function getUserGroupById($id){
		$sql = "select * from tbusergroup where id = ?";
		$query = $this->db->query($sql,array("$id"));
		$arr = array();
		foreach ($query->result() as $row){
			$arr["id"] = $row->id;
			$arr["name"] = $row->name;			
		}
		return $arr;
	}	
	function getUserGroupNameById($id){
		$sql = "select * from tbusergroup where id = ?";
		$query = $this->db->query($sql,array("$id"));
		
		foreach ($query->result() as $row){
			return $row->name;
		}
		return "";
	}
	function addUserGroup($name){
		$sql = "insert into tbusergroup(name,createtime) values(?,now())";
		$this->db->query($sql,array("$name"));
		return $this->db->affected_rows();
	}
	function updateUserGroup($id,$name){
		$sql = "update tbusergroup set name = ? where id = ?";
		$this->db->query($sql,array("$name","$id"));
		return $this->db->affected_rows();
	}
	
}
?>